Skip to content

Use more precise return types in asyncio methods#6937

Merged
Akuli merged 1 commit intopython:masterfrom
drtyrsa:specific_asyncio_proto
Jan 17, 2022
Merged

Use more precise return types in asyncio methods#6937
Akuli merged 1 commit intopython:masterfrom
drtyrsa:specific_asyncio_proto

Conversation

@drtyrsa
Copy link
Contributor

@drtyrsa drtyrsa commented Jan 17, 2022

No description provided.

@github-actions
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

websockets (https://github.com/aaugustin/websockets)
+ src/websockets/legacy/client.py:641: error: "_ProtocolT" has no attribute "close"
+ src/websockets/legacy/client.py:655: error: Incompatible types in assignment (expression has type "WebSocketClientProtocol", variable has type "_ProtocolT")
+ src/websockets/legacy/client.py:659: error: "_ProtocolT" has no attribute "handshake"
+ src/websockets/legacy/client.py:661: error: "_ProtocolT" has no attribute "origin"
+ src/websockets/legacy/client.py:662: error: "_ProtocolT" has no attribute "available_extensions"
+ src/websockets/legacy/client.py:663: error: "_ProtocolT" has no attribute "available_subprotocols"
+ src/websockets/legacy/client.py:664: error: "_ProtocolT" has no attribute "extra_headers"
+ src/websockets/legacy/client.py:667: error: "_ProtocolT" has no attribute "fail_connection"
+ src/websockets/legacy/client.py:668: error: "_ProtocolT" has no attribute "wait_closed"
+ src/websockets/legacy/client.py:672: error: Incompatible return value type (got "_ProtocolT", expected "WebSocketClientProtocol")

anyio (https://github.com/agronholm/anyio)
+ src/anyio/_backends/_asyncio.py:1365: error: Redundant cast to "StreamProtocol"  [redundant-cast]
+ src/anyio/_backends/_asyncio.py:1555: error: Redundant cast to "DatagramProtocol"  [redundant-cast]

aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/connector.py:1295: error: Redundant cast to "ResponseHandler"  [redundant-cast]

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, the anyio and aiohttp primer output looks promising. websockets is doing some complicated stuff when creating protocols, so I think they'd just need to update their annotations. I'd like a second opinion, though.

@Akuli
Copy link
Collaborator

Akuli commented Jan 17, 2022

websockets does this:

        if kwargs.pop("unix", False):
            ...
            create_connection = functools.partial(
                loop.create_unix_connection, factory, path, **kwargs
            )
        else:
            ...
            create_connection = functools.partial(
                loop.create_connection, factory, host, port, **kwargs
            )
        ...
        self._create_connection = create_connection

Code like this isn't very typing friendly, and I don't think there's a good way for us to support it anyway. Websockets already has casts.

@Akuli Akuli merged commit e558fed into python:master Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants